package com.atguigu.rabbitmq.five;
import com.atguigu.rabbitmq.util.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.rabbitmq.client.Delivery;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 消息接收
 */
public class ReceiveLogs01 {

    //交换机的名字
    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChanner();

        //声明一个交换机
        //参数1:交换机名称
        //参数2:交换机类型
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

        //声明一个队列 临时队列
        /*
         * 生成一个临时的队列,队列的名称是随机的
         * 当消费者断开与队列的连接的时候,队列就自动删除
         */
        String queue = channel.queueDeclare().getQueue();

        /*
         *绑定交换机与队列
         * 参数一:队列名称
         */
        channel.queueBind(queue, EXCHANGE_NAME, "");
        System.out.println("等待接收消息,把接收到的消息打印在屏幕上......");

        //接收消息
        DeliverCallback deliverCallback = (String consumerTag, Delivery message) -> {
            System.out.println("控制台打印接收到的消息:" + new String(message.getBody(), "UTF-8"));
        };


        channel.basicConsume(queue, true, deliverCallback, (message) -> {
            System.out.println("消费者取消接口");
        });
    }

}